package com.sky.controller.user;

import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.ShoppingCart;
import com.sky.result.Result;
import com.sky.service.ShopCartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/user/shoppingCart")
@Api("购物车管理接口")
public class ShopCartController {
    @Autowired
    private ShopCartService shopCartService;

    @PostMapping("/add")
    @ApiOperation("添加商品到购物车")
    public Result<String> add(@RequestBody ShoppingCartDTO shoppingCartDTO) {
        log.info("添加商品到购物车: {}", shoppingCartDTO);
        shopCartService.add(shoppingCartDTO);
        return Result.success();
    }

    @GetMapping("/list")
    @ApiOperation("查看购物车")
    public Result<List<ShoppingCart>> showShopCart() {
        log.info("查看购物车");
        List<ShoppingCart> shoppingCartList = shopCartService.showShopCart();
        return Result.success(shoppingCartList);
    }

    @PostMapping("/sub")
    @ApiOperation("删除购物车中的一件商品")
    public Result<String> deleteOne(@RequestBody ShoppingCartDTO shoppingCartDTO) {
        log.info("删除购物车中的一件商品: {}", shoppingCartDTO);
        shopCartService.deleteOne(shoppingCartDTO);
        return Result.success();
    }

    @DeleteMapping("/clean")
    @ApiOperation("清空购物车")
    public Result<String> clean() {
        log.info("清空购物车");
        shopCartService.clean();
        return Result.success();
    }


}
